/**
 * Copyright (c) 2017-2017 All Rights Reserved.
 */
package com.zbest.zpay.paycore.biz.action.pay;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zbest.zpay.paycore.dal.dao.PayInfoDAO;
import com.zbest.zpay.paycore.dal.dao.PaySubInfoDAO;
import com.zbest.zpay.paycore.dal.model.PaySubInfoDO;
import com.zbest.zpay.paycore.helper.PayInfoStatusManager;
import com.zbest.zpay.paycore.model.PayInfoStatus;
import com.zbest.zpay.paycore.model.PaySubInfoStatus;
import com.zbest.zpay.service.paycore.model.PaycoreKeyEnum;
import com.zbest.zpay.shared.process.BaseAction;
import com.zbest.zpay.shared.process.ProcessResult;

/**
 * 
 *
 * @author Administrator
 * @version $Id: PayInfoAdvanceAction.java, v 0.1 2017-6-4 上午12:14:08 Administrator Exp $
 */
@Service("payInfoAdvanceAction")
public class PayInfoAdvanceAction extends BaseAction {

    @Resource
    private PayInfoDAO           payInfoDAO;

    @Resource
    private PaySubInfoDAO        paySubInfoDAO;

    @Resource
    private PayInfoStatusManager payInfoStatusManager;

    /** 
     * @see com.zbest.zpay.shared.process.BaseAction#perform(com.zbest.zpay.shared.process.ProcessResult)
     */
    @Override
    public void perform(ProcessResult processResponse) {

        PaySubInfoDO paySubInfoDO = getValue(PaycoreKeyEnum.PAY_SUB_INFO_DO.getCode());
        List<PaySubInfoDO> paySubInfoDOList = paySubInfoDAO.queryList(paySubInfoDO.getPayInfoId());
        if (paySubInfoDO.getStatus().equals(PaySubInfoStatus.SUCCESS.getCode())) {
            for (PaySubInfoDO subInfoDO : paySubInfoDOList) {
                if (!subInfoDO.getStatus().equals(PaySubInfoStatus.SUCCESS.getCode())) {
                    //TODO 未来如果有多种支付工具，支付子单状态还没成功，推进
                    throw new UnsupportedOperationException();
                }
            }
            payInfoStatusManager.statusCheck(PayInfoStatus.PROCESS.getCode(),
                PayInfoStatus.SUCCESS.getCode());
            payInfoDAO.updateStatus(paySubInfoDO.getPayInfoId(), PayInfoStatus.SUCCESS.getCode(),
                PayInfoStatus.PROCESS.getCode());
        } else if (paySubInfoDO.getStatus().equals(PaySubInfoStatus.FAIL.getCode())) {
            for (PaySubInfoDO subInfoDO : paySubInfoDOList) {
                if (!subInfoDO.getStatus().equals(PaySubInfoStatus.FAIL.getCode())) {
                    //TODO 未来如果有多种支付工具，支付子单状态还没失败，发起失败处理
                    throw new UnsupportedOperationException();
                }
            }
            payInfoStatusManager.statusCheck(PayInfoStatus.PROCESS.getCode(),
                PayInfoStatus.FAIL.getCode());
            payInfoDAO.updateStatus(paySubInfoDO.getPayInfoId(), PayInfoStatus.FAIL.getCode(),
                PayInfoStatus.PROCESS.getCode());
        }

    }
}
